package com.binguo.thirdPay.HengDaLvYouPay;

import com.alibaba.fastjson.JSONObject;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author yuritian
 * @Title: HengDaLvYouPay
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020/4/2914:06
 */
@Service
public class HengDaLvYouPayService {

    public static void main(String[] args) {
        HengDaLvYouPayService hengDaLvYouPay = new HengDaLvYouPayService();
        /*List<HDLYDishVO> dishVOList = new ArrayList<>();
        HDLYDishVO dishVO = new HDLYDishVO();
        dishVO.setsDocAdd_c("1");
        dishVO.setsDish_c("1");
        dishVO.setsDish_n("广式烧乳猪");
        dishVO.setnUnitPrice("1");
        dishVO.setnQty_Down("1");
        dishVO.setsUnit("份");
        dishVOList.add(dishVO);
        hengDaLvYouPay.downDoc("100000","YunPengTest003",dishVOList);*/
        //hengDaLvYouPay.getDishImageLastTime("100059");
        hengDaLvYouPay.getBaseInfo("100000","swt_jDish");
        //hengDaLvYouPay.getBaseInfo();
    }

    /**
     * @Author: yujing
     * @Description:
     * @param
     * @DateTime: 2020/4/30 10:02
     * @Return void
     */
    public void getBaseInfo(){
        JSONObject result = new JSONObject();
        JSONObject sbody = new JSONObject();
        sbody.put("sTableName","swt_jShop");
        result = getRessultJson(sbody,"https://lycfc.evergrande.com:17712/WAC/SWT/DCOnlineOrderWS/GetBaseInfo");
        //return result;
    }

    /**
     * @Author: yujing
     * @Description: 根据门店编码获取请求内容名相关内容
     * @param shopC 门店编码
     * @param sTableName 请求内容名
     * @DateTime: 2020/4/30 10:02
     * @Return JSONObject
     */
    public JSONObject getBaseInfo(String shopC,
                                  String sTableName){
        JSONObject result;
        JSONObject sbody = new JSONObject();
        sbody.put("sShop_c",shopC);
        sbody.put("sTableName",sTableName);
        result = getRessultJson(sbody,"https://lycfc.evergrande.com:17712/WAC/SWT/DCOnlineOrderWS/GetBaseInfo");
        JSONObject shead = result.getJSONObject("SHEAD");
        JSONObject ret = shead.getJSONObject("ret");
        if (ret.getString("retCode").equals("000000")){
            return result.getJSONObject("SBODY");
        }else {
            return null;
        }
    }

    /**
     * @Author: yujing
     * @Description: 查询菜品图片的最后修改时间
     * @param shopC 门店编码
     * @DateTime: 2020/4/30 10:15
     * @Return void
     */
    public void getDishImageLastTime(String shopC){
        JSONObject result = new JSONObject();
        JSONObject sbody = new JSONObject();
        sbody.put("sShop_c",shopC);
        sbody.put("sTableName","swt_jDish");
        result = getRessultJson(sbody,"https://lycfc.evergrande.com:17712/WAC/SWT/DCOnlineOrderWS/GetDishImageLastTime");
    }

    /**
     * @Author: yujing
     * @Description: 订单下单
     * @param shopC 门店编码
     * @param orderDoc 订单号
     * @param dishVOList 菜品列表
     * @DateTime: 2020/5/4 11:19
     * @Return void
     */
    public void downDoc(String shopC,
                        String orderDoc,
                        List<HDLYDishVO> dishVOList){
        JSONObject result = new JSONObject();
        JSONObject sbody = new JSONObject();
        sbody.put("sShop_c",shopC);
        sbody.put("sOrderDoc_c",orderDoc);
        sbody.put("DocDish",dishVOList);
        result = getRessultJson(sbody,"https://lycfc.evergrande.com:17712/WAC/SWT/web"+shopC+"/DownDoc");

    }

    /**
     * @Author: yujing
     * @Description:
     * @param shopC 店铺编号
     * @param orderDoc 订单号
     * @param doc 消费单号
     * @param paymentc 付款编码
     * @param paymentn 付款名称
     * @param paymoney 付款金额
     * @DateTime: 2020/5/4 13:44
     * @Return void
     */
    public void payDoc(String shopC,
                       String orderDoc,
                       String doc,
                       String paymentc,
                       String paymentn,
                       String paymoney){
        JSONObject result = new JSONObject();
        JSONObject sbody = new JSONObject();
        sbody.put("sShop_c",shopC);
        sbody.put("sOrderDoc_c",orderDoc);
        sbody.put("sDoc_c",doc);
        sbody.put("sPayment_c",paymentc);
        sbody.put("sPayment_n",paymentn);
        sbody.put("sPaymoney",paymoney);
        result = getRessultJson(sbody,"https://lycfc.evergrande.com:17712/WAC/SWT/web"+shopC+"/PayDoc");
    }

    /**
     * @Author: yujing
     * @Description:
     * @param shopC
     * @param orderDoc
     * @param doc
     * @DateTime: 2020/5/4 14:18
     * @Return void
     */
    public void cancelDoc(String shopC,
                          String orderDoc,
                          String doc){
        JSONObject result = new JSONObject();
        JSONObject sbody = new JSONObject();
        sbody.put("sShop_c",shopC);
        sbody.put("sOrderDoc_c",orderDoc);
        sbody.put("sDoc_c",doc);
        result = getRessultJson(sbody,"https://lycfc.evergrande.com:17712/WAC/SWT/web"+shopC+"/CancelDoc");

    }

    /**
     * @Author: yujing
     * @Description: 请求第三接口获取相关数据
     * @param sbody 请求内容名
     * @DateTime: 2020/4/30 10:04
     * @Return com.alibaba.fastjson.JSONObject
     */
    private JSONObject getRessultJson(JSONObject sbody,
                                      String postUrl){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        JSONObject postObject = new JSONObject();
        JSONObject shead = new JSONObject();
        shead.put("requestId","HDHHD");
        shead.put("targetId,","SWT");
        shead.put("serviceId","");
        shead.put("timestamp",new Date());
        shead.put("userId","");
        shead.put("password","");
        shead.put("seqNo","");
        shead.put("transNo","");
        postObject.put("SHEAD",shead);
        postObject.put("SBODY",sbody);
        String message = postObject.toJSONString();
        System.out.println(message);
        HttpEntity<String> entity = new HttpEntity<String>(message, headers);
        ResponseEntity<String> resp = restTemplate.postForEntity(postUrl, entity, String.class);
        String result = resp.getBody();
        System.out.println(result);
        return JSONObject.parseObject(result);
    }

}
